package com.hey.function;

import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;

public class MuFunction {

    public Flux first() {
        HandlerFunction<ServerResponse> timeFunction =
                request -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(
                        Mono.just("now is " + new SimpleDateFormat("HH:mm:ss").format(new Date())), String.class);
        HandlerFunction<ServerResponse> dateFunction =
                request -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(
                        Mono.just("Today is " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())), String.class);
        RouterFunction<ServerResponse> router =
                RouterFunctions.route(GET("/time"), timeFunction)
                        .andRoute(GET("/Date"), dateFunction);
        return Flux.just(timeFunction, dateFunction);
    }
}
